package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ExamPaperTypeMapper;
import com.think.teaching.model.ExamPaperType;
import com.think.teaching.service.ExamPaperTypeService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamPaperTypeServiceImpl extends BaseServiceImpl<ExamPaperTypeMapper, ExamPaperType>
		implements ExamPaperTypeService {

	@Override
	public ExamPaperType AddExamPaperType(ExamPaperType examPaperType) {
		if (this.save(examPaperType)) {
			return examPaperType;
		}
		return null;
	}

	@Override
	public ExamPaperType UpdateExamPaperType(ExamPaperType examPaperType) {
		if (this.updateById(examPaperType)) {
			return examPaperType;
		}
		return null;
	}

	@Override
	public ExamPaperType GetExamPaperType(Integer id) {
		QueryWrapper<ExamPaperType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public List<ExamPaperType> SearchExamPaperTypeWithKey(String key, Boolean withoutBan) {
		QueryWrapper<ExamPaperType> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("type_name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	@Override
	public IPage<ExamPaperType> ListExamPaperTypeWithTagAndType(Integer count, Integer page, Integer type, Integer tag,
			Boolean withoutBan) {
		QueryWrapper<ExamPaperType> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<ExamPaperType> ipage = new Page<>(page, count);
		return this.page(ipage, queryWrapper);
	}

	@Override
	public ExamPaperType SetExamPaperTypeState(Integer id, Integer state) {
		UpdateWrapper<ExamPaperType> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}
	public ExamPaperType GetExamPaperTypeByName(String name) {
		QueryWrapper<ExamPaperType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("type_name", name).ne("delflag", 1);
		return getOne(queryWrapper);
	}
}
